﻿package server;

import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BankLAccList {
	private Data data;
	
	public BankLAccList(){
		data=new Data();
	}
	
	public int getLastLANum(int pID){
		String sql;
		ResultSet rs=null;
		int lastLANum=1;
		
		sql="select * from lacclist where pID="+pID+" order by laNum";
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				lastLANum=rs.getInt("laNum");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return lastLANum;
	}
	
	public int getFirstLANum(int pID){
		String sql;
		ResultSet rs=null;
		int firstLANum=-1;
		
		sql="select * from lacclist where pID="+pID+" order by laNum";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				firstLANum=rs.getInt("laNum");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return firstLANum;
	}
	
	public int getNextLANum(int pID,int laNum){
		String sql;
		ResultSet rs=null;
		int nextLANum=-1;
		
		sql="select * from lacclist where pID="+pID+" order by laNum";
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				if(rs.getInt("laNum")==laNum){
					rs.next();
					nextLANum=rs.getInt("laNum");
				}
			}
		} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return nextLANum;
	}
	
	public int addLA(int pID,double LASum,int limit,Date startTime){//���һ�ʴ��
		//����һ��ϵͳ��ɵĴ�����LANum
		String sql;
		int re=0;
		String time=new SimpleDateFormat("yyyy-MM-dd").format(startTime);
		int LANum=getLastLANum(pID)+1;
		BankAccList bankacclist=new BankAccList();
		int CANum=bankacclist.getCANum(pID);
		
		sql="insert into lacclist values (null,"+pID+","+LANum+","+LASum+","+limit+",'"+time+"')";
		re=data.update(sql);
		
		if(re==1 && bankacclist.setCABalance(CANum, bankacclist.getCABalance(CANum)+LASum)){
			return LANum;
		}
		return -1;
	}
	
	  public double getLASum(int pID,int LANum){
		//�����˺�FANum�����ش���FASum
		String sql;
		ResultSet rs=null;
		double LASum=0;
		
		sql="select * from lacclist where pID="+pID+" and laNum="+LANum ;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				LASum=rs.getDouble("laSum");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return LASum;
		}
	  
	  public int getLimit(int pID,int LANum){
		  String sql;
			ResultSet rs=null;
			int limit=0;
			
			sql="select * from lacclist where pID="+pID+" and laNum="+LANum+" limit 1";
			rs=data.getResultSet(sql);
			try {
				while(rs.next()){
					limit=rs.getInt("laLimit");
			}
				} catch (SQLException e) {
			
				e.printStackTrace();
				}
				
			return limit;
		}
	  
	  public boolean deleLA(int pID,int LANum){	
		//����ɾ���˱ʴ���������Ϣ
		String sql;
		int re=0;
		
		sql="delete from lacclist where pID="+pID+" and laNum="+LANum;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
		}
	  
	  public String getStartTime(int pID,int LANum){
		  String sql;
			ResultSet rs=null;
			String time="";
			
			sql="select * from lacclist where pID="+pID+" and laNum="+LANum+" limit 1";
			rs=data.getResultSet(sql);
			try {
				while(rs.next()){
					time=rs.getString("laStartTime");
			}
				} catch (SQLException e) {
			
				e.printStackTrace();
				}
				
			return time;
	  }
	  
	  public int getSize(){
	    	String sql;
			ResultSet rs=null;
			int sum=0;
			
			sql="select count(*) as sum from lacclist";
			rs=data.getResultSet(sql);
			try {
				if(rs.next()){
					sum=rs.getInt("sum");
			}
				} catch (SQLException e) {
			
				e.printStackTrace();
				}
				
			return sum;
	    }
	  
	  public int getpSize(int p)  throws RemoteException{
	    	String sql;
			ResultSet rs=null;
			int sum=0;
			
			sql="select count(*) as sum from lacclist where pID="+p;
			rs=data.getResultSet(sql);
			try {
				if(rs.next()){
					sum=rs.getInt("sum");
			}
				} catch (SQLException e) {
			
				e.printStackTrace();
				}
				
			return sum;
	    }
}
